﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Model;
using BLL;
using System.Configuration;
using System.Data;

public partial class Information_EditInfo : BasePage
{
    Access access = new Access();
    informationBO ibo = new informationBO();
    WeiMenuBO wbo = new WeiMenuBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = wbo.GetList(" mid=" + CurrentUserInfo.MID + "  and Keys<>'' and type=1 ").Tables[0];
            ddlKey.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ddlKey.Items.Add(new ListItem(dr["Keys"].ToString(), dr["Keys"].ToString()));
            }
            if (GetQeuryString("para") == "update")
            {
                InitData();
            }
            else if (GetQeuryString("para") == "add")
            {
                //this.txtCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
            ddlMsgType_SelectedIndexChanged(null, null);
            ddlreplyType_SelectedIndexChanged(null, null);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {


            if (GetQeuryString("para") == "update")
            {
                Model.information menu = GetEntity();
                menu.ID = Convert.ToInt32(GetQeuryString("id"));
                ibo.Update(menu);
                AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
            }
            else if (GetQeuryString("para") == "add")
            {
                Model.information m = GetEntity();
                ibo.Add(m);
                AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
            }
        }
        catch (Exception)
        {
            AlertMessage("请将信息填写完整");
        }
    }

    private void InitData()
    {
        string id = GetQeuryString("id");
        information ds = ibo.GetModel(int.Parse(id));
        txtContent.Text = ds.Content;
        txtDescription.Text = ds.Description;
        txtGBUrl.Text = ds.fulltextUrl;
        txtKeyWords.Text = ds.KeyWords;
        txtPicUrl.Text = ds.PicUrl;
        txtTitle.Text = ds.Title;
        ddlMsgType.SelectedValue = ds.MsgType;
        ddlreplyType.SelectedValue = ds.replyType.ToString();
        ddlKey.SelectedValue = ds.KeyWords;
    }
    public Model.information GetEntity()
    {
        Model.information m = new Model.information();
        if (GetQeuryString("para") == "update")
        {
            m = ibo.GetModel(Convert.ToInt32(GetQeuryString("id")));
        }
        m.Content = txtContent.Text;
        m.CreateTime = DateTime.Now.ToShortDateString();
        m.Description = txtDescription.Text;
        m.fulltextUrl = txtGBUrl.Text;
        m.KeyWords = txtKeyWords.Text;
        m.Label = "";
        m.Location_X = "";
        m.Location_Y = "";
        m.MsgId = "";
        m.MsgType = ddlMsgType.SelectedValue;
        m.PicUrl = txtPicUrl.Text;
        m.replyType = int.Parse(ddlreplyType.SelectedValue);
        m.Scale = "";
        m.Title = txtTitle.Text;
        m.MID = CurrentUserInfo.MID;
        if (ddlreplyType.SelectedValue == "4")
            m.KeyWords = ddlKey.SelectedValue;

        return m;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string FileName = file.FileName;
        string path = HttpContext.Current.Request.MapPath("files/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg" || extension.ToLower() == ".mp3"))
            {
                int fileSize = file.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                //else if (fileSize > 52100)
                //{
                //    AlertMessage("请上传50K以内的JPG格式照片");
                //}
                else
                {
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        file.SaveAs(path + guid + extension.ToLower());  //将文件上传到服务器
                        if (File.Exists(path + guid + extension.ToLower()))
                        {
                          //  Image1.ImageUrl = "files/" + guid + extension.ToLower();
                            txtPicUrl.Text = ConfigurationManager.AppSettings["ServerURL"] + "/Information/files/" + guid + extension.ToLower();
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG或MP3格式文件!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
        //}
    }

    protected void ddlMsgType_SelectedIndexChanged(object sender, EventArgs e)
    {
        tr1.Visible = tr2.Visible = tr3.Visible = tr4.Visible = tr5.Visible = tr6.Visible = false;
        if (ddlMsgType.SelectedValue == "text")
        {
            tr6.Visible = true;
            //tr7.Visible = true;
        }
        else if (ddlMsgType.SelectedValue == "news")
        {
            tr1.Visible = tr2.Visible = tr3.Visible = tr4.Visible = tr5.Visible = true;
        }
        else if (ddlMsgType.SelectedValue == "music")
        {
            tr1.Visible = tr2.Visible = tr3.Visible = tr4.Visible  = true;
        }
    }
    protected void ddlreplyType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlreplyType.SelectedValue == "1")
            tr7.Visible = true;
        else
            tr7.Visible = false;

        if (ddlreplyType.SelectedValue == "4")
            tr8.Visible = true;
        else
            tr8.Visible = false;
    }
}